<?php

namespace app\admin\controller;

use app\admin\library\Excel;
use app\common\controller\Backend;
use think\facade\Db;
use think\db\exception\PDOException;
use think\exception\ValidateException;

/**
 * 司押人员信息
 *
 */
class XDriver extends Backend
{
    /**
     * XDriver模型对象
     * @var \app\admin\model\XDriver
     */
    protected $model = null;
    
    protected $quickSearchField = ['IDCARD'];

    protected $defaultSortField = 'IDCARD,desc';
    protected $noNeedLogin      = ['*'];
    protected $preExcludeFields = [''];

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\XDriver;
    }


    public function importData()
    {
        // 接收文件上传信息
        $files = $this->request->file("file");
        $CORP=$this->request->post('corp');
        // 调用类库，读取excel中的内容
        $data = Excel::importExcel($files);
        $k=['0'=>'','1'=>'','2'=>'','3'=>''];
        $data=array_diff_key($data,$k);
        $add=[];
        $adminInfo          = $this->auth->getInfo();
        foreach ($data as $k=>$datum) {
            $add[$k]['CORP']=$CORP;
            $add[$k]['FULLNAME']=$datum[1];
            $add[$k]['IDCARD']=$datum[2];
            $add[$k]['BIRTHDAY']=date('Y-m-d', ($datum[3] - 25569) * 24 * 3600);
            $add[$k]['TJDATE']=date('Y-m-d', ($datum[5] - 25569) * 24 * 3600);
            $add[$k]['TJDEMO']=$datum[6];
            $add[$k]['CARTYPE']=$datum[7];
            $add[$k]['FIRSTDATE']=date('Y-m-d', ($datum[8] - 25569) * 24 * 3600);;
            $add[$k]['JSZDATE']=date('Y-m-d', ($datum[10] - 25569) * 24 * 3600);;;
            $add[$k]['CYZGID']=$datum[11];
            $add[$k]['CYZGDATE']=date('Y-m-d', ($datum[12] - 25569) * 24 * 3600);;
            $add[$k]['STATUS']='1';
            $add[$k]['DEMO']=$datum[13];
            $add[$k]['OP']=$adminInfo['nickname'];
            $add[$k]['OPTIME']=date('Y-m-d H:i:s',time());
        }
        $result = false;
        Db::startTrans();
        try {

            $result=$this->model->insertAll($add);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success(__('Added successfully'));
        } else {
            $this->error(__('No rows were added'));
        }
    }
}